import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class Test {
    public List<List<Integer>> genarate(int num){
        List<List<Integer>> list=new ArrayList<>() ;
        List<Integer> row1=new ArrayList<>();
        row1.add(1);
        list.add(row1);
        for (int i = 1; i < num; i++) {
            List<Integer> row=new ArrayList<>();
            row.add(1);
            List<Integer> prerow=list.get(i-1);
            for (int j = 1; j < i; j++) {
                Integer sum=prerow.get(j)+prerow.get(j-1);
                row.add(sum);
            }
            row.add(1);
            list.add(row);
        }
        return list;
    }
    public static void main(String[] args) {
        Test test=new Test();
        int num=6;
       List<List<Integer>> pascalTriangle=test.genarate(num);
        System.out.println("杨辉三角 （"+num+"行）：");
        for (List<Integer> row: pascalTriangle) {
            for (int number:row) {
                System.out.print(number+" ");
            }
            System.out.println();
        }
        System.out.println("\n列表形式：");
        for (List<Integer> row:
            pascalTriangle) {
            System.out.println(row);
        }

    }
    public static void main3(String[] args) {
        ArrayList<Integer> arrayList=new ArrayList<>();
        arrayList.add(1);
        arrayList.add(2);
        arrayList.add(3);
        arrayList.add(4);
        arrayList.add(5);
        //遍历方法1
        System.out.println(arrayList);
        //遍历方法2
        for (int i = 0; i < arrayList.size(); i++) {
            System.out.print(arrayList.get(i)+" ");
        }
        System.out.println();
        //遍历方法3
        for (int x:arrayList) {
            System.out.print(x+" ");
        }
        System.out.println();
        //遍历方法4迭代器Iterator
        Iterator<Integer> it=arrayList.iterator();
        while (it.hasNext()){
            System.out.print(it.next()+" ");
        }
        System.out.println();
        //遍历方法5迭代器ListIterator
        ListIterator<Integer> it1=arrayList.listIterator();
        while (it1.hasNext()){
            System.out.print(it1.next());
        }
        System.out.println();
        ListIterator<Integer> it2=arrayList.listIterator();
        while (it2.hasPrevious()){
            System.out.print(it2.previous()+" ");
        }
        System.out.println();
    }
    public static void main2(String[] args) {
        ArrayList<Integer> arrayList=new ArrayList<>();
        arrayList.add(1);
        arrayList.add(2);
        arrayList.add(3);
        arrayList.add(3);
        arrayList.add(3);
        arrayList.add(4);
        arrayList.add(5);
        arrayList.add(36);
        System.out.println(arrayList);
        List<Integer> list=arrayList.subList(1,4);//[1,4)
        System.out.println(list);
         list.set(0,12);
        System.out.println(list);
        System.out.println(arrayList);           //重点
        arrayList.add(0,99);
        System.out.println(arrayList);
        arrayList.remove(new Integer(99));// arrayList.remov(0)    不一样
        System.out.println(arrayList);

    }

    public static void main1(String[] args) {
        MyMarrayList myMarrayList=new MyMarrayList();
        myMarrayList.add(1);
        myMarrayList.add(2);
        myMarrayList.add(3);
        myMarrayList.add(4);
        myMarrayList.add(5);
        myMarrayList.add(1212);
        try {
            myMarrayList.add(1,12);
        }catch (PosOutOfBoundsException e){
            e.printStackTrace();
        }
try {
    int a=myMarrayList.get(1);
    System.out.println(a);
}catch (LisyEmptException e){
    e.printStackTrace();
}
        myMarrayList.display();
        System.out.println("----------------");
        myMarrayList.set(1,22);
        myMarrayList.display();
        System.out.println("----------------");
        System.out.println(myMarrayList.contains(22));
        System.out.println(myMarrayList.contains(55));
        System.out.println("----------------");
        System.out.println(myMarrayList.indexOf(22));
        System.out.println("----------------");
        myMarrayList.remove(1);
        myMarrayList.display();
        System.out.println("----------------");
myMarrayList.remove(123);
        System.out.println(myMarrayList.size());
    }

}
